package com.example.userlogincenter.userCenter.controller;
import com.example.userlogincenter.userCenter.common.unifyDataForm.Result;
import com.example.userlogincenter.userCenter.common.unifyDataForm.ResultVo;
import com.example.userlogincenter.userCenter.entity.ApikeyInfo;
import com.example.userlogincenter.userCenter.entity.CenterUser;
import com.example.userlogincenter.userCenter.service.ApikeyInfoService;
import com.example.userlogincenter.userCenter.service.ICenterUserService;
import com.example.userlogincenter.userCenter.service.KeyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import java.util.HashMap;

@RestController
@CrossOrigin
@RequestMapping("/access")
@EnableOpenApi
@Api(tags = "Key控制器")
public class KeyController {



    @Autowired
    private ApikeyInfoService apikeyInfoService;


    @Autowired
    private ICenterUserService userService;

    @ApiOperation("获取签名")
    @PostMapping ("getKey")
    public Result getKey(@RequestBody ApikeyInfo apikeyInfo){
        boolean saveKey = apikeyInfoService.saveKey(apikeyInfo);
        if (saveKey){
            return Result.success();
        }
        return Result.error(ResultVo.ERROR_UNKOWN);
    }


    /**
     *
     * 删除签名 Controller
     *
     * **/
    @ApiOperation(value="删除签名")
    @PostMapping("delKey")
    public Result updateKey(@RequestBody ApikeyInfo apikeyInfo){
        boolean isDel = apikeyInfoService.removeById(apikeyInfo.getAkId());
        if (isDel){
            return Result.success("删除accessKey成功！");
        }
        return Result.error(ResultVo.ERROR_UNKOWN);
    }

    /**
     *
     * 查看签名 Controller
     *
     * */
    @ApiOperation(value = "查询签名")
    @PostMapping("/lookupKey")
    public Result SearchKey(@RequestParam("userid") String userid,@RequestBody String verfication){
        ApikeyInfo apikeyInfo = apikeyInfoService.lookupKey(userid);
        String accessKey = apikeyInfo.getAkKey();
        String asscessSecret = apikeyInfo.getAkSecret();
        HashMap<String,String> accessVerification = new HashMap<String,String>();
        accessVerification.put("accessKey",accessKey);
        accessVerification.put("asscessSecret",asscessSecret);
        return Result.success(accessVerification);
    }


    /*
    *
    * 删除密钥签名
    *
    * */
    @GetMapping("DelKey/{akid}")
    public Result delKey(@PathVariable("akid")Long akid){
        boolean result = apikeyInfoService.removeById(akid);
        if (result){
            return Result.success("删除成功！");
        }else{
            return Result.error(ResultVo.ERROR_UNKOWN);
        }
    }
}
